﻿using System;
using EPiServer;
using EPiServer.Core;
using EPiServer.Editor;
using EPiServer.SpecializedProperties;
using EPiServer.Web.PropertyControls;

namespace TemplateFoundation.UI.WebControls
{
    internal abstract class PropertyUrlControlBase : PropertyUrlControl
    {
        public PropertyUrlControlBase()
        {
            RenderType = RenderType.Edit;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            EditControl = new System.Web.UI.WebControls.TextBox { MaxLength = 0xff };
        }

        protected override string CreateLinkEditorScript()
        {
            string str = UriSupport.ResolveUrlFromUIBySettings("editor/dialogs/HyperlinkProperties.aspx");
            string str2 = HtmlEditor.GetFileManagerBrowserUrl(CurrentPage!=null ? CurrentPage.PageLink : PageReference.StartPage, CurrentPage!=null ? CurrentPage.ParentLink : PageReference.RootPage, (int)CurrentPage.Property["PageFolderID"].Value);
            return ("\r\n\t\t\t<script type='text/javascript'>\r\n\t\t\t// <![CDATA[\r\n\t\t\t\tfunction PropertyUrl_DoPickup(obj,type)\r\n\t\t\t\t{\r\n\t\t\t\t\tvar node = document.getElementById(obj);\r\n\t\t\t\t\tvar browserselectionmode = '';\r\n                    var callbackMethod;\r\n\r\n                    if (type == 128)\r\n                    {\r\n                        browserselectionmode = '&browserselectionmode=image'\r\n                    }\r\n                    else if (type == 129)\r\n                    {\r\n                        browserselectionmode = '&browserselectionmode=file'\r\n                    }\r\n                    \r\n                    if (browserselectionmode != '')\r\n                    {\r\n                        callbackMethod = function(returnValue, callbackArgs)\r\n                                                {\r\n                                                    if (returnValue)\r\n                                                    {\r\n                                                        if (returnValue.closeAction == 'clear')\r\n                                                        {\r\n                                                            callbackArgs.value = '';\r\n                                                        }\r\n                                                        else if (returnValue.items && returnValue.items.length == 1)\r\n                                                        {\r\n                                                            callbackArgs.value = returnValue.items[0].path;\r\n                                                        }\r\n                                                        EPi.DispatchEvent(callbackArgs, 'change');\r\n                                                    }\r\n                                                }\r\n                        var dialogUrl = '" + str2 + "' + browserselectionmode + '&selectedfile=' + encodeURIComponent(node.value);\r\n                        EPi.CreateDialog(dialogUrl, callbackMethod, node, null, {width: 600});\r\n                    }\r\n                    else\r\n                    {\r\n                        var linkObject = new Object();\r\n\t\t\t\t\t\tif (node && node.value.length > 0)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tvar href = node.value;\r\n\t\t\t\t\t\t\tlinkObject.href\t= href;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tlinkObject.onlyUrl = true;\t\r\n\t\t\t\t\t\tlinkObject.fileManagerBrowserUrl = '" + str2 + "';\r\n\t\t\t\t\t\t\r\n                        var callbackMethod = function(returnValue, callbackArgs)\r\n                                                {\r\n                                                    if (returnValue)\r\n                                                    {\r\n                                                        if (returnValue == -1)\r\n                                                        {\r\n                                                            callbackArgs.value = '';\r\n                                                        }\r\n                                                        else if (returnValue!=0)\r\n                                                        {\r\n                                                            callbackArgs.value=returnValue.href;  \r\n                                                        }\r\n                                                        EPi.DispatchEvent(callbackArgs, 'change');\r\n                                                    }\r\n                                                }\r\n                        EPi.CreateDialog('" + str + "?url='+ encodeURIComponent(node.value), callbackMethod, node, linkObject, {width:460, height:260, scrollbars: 'no'});                      \r\n                    }\r\n                }\r\n            // ]]>\r\n\t\t\t</script>\r\n\t\t\t");
        }

        public string ValueControlId
        {
            get
            {
                return EditControl.ClientID;
            }
        }

        protected string LinkUrl
        {
            get
            {
                return EditControl.Text;
            }
            set
            {
                PropertyData.LoadData(value);

                if (EditControl!=null)
                {
                    EditControl.Text = value;
                }
            }
        } 
    }
}
